
class JobStatus():
    Idle = 0
    WaitingToRun = 1
    Runing = 2
    RunToCompletion = 3
    Canceling = 4
    Canceled  = 5
    NoRun  = 6

class JobResult():
    Unavailable = 0
    Success = 1
    Error = 2
    Exception = 3    
    
class Job():
    def __init__(self):
        self.start_handler = None
        self.stop_handler = None
        self.status = JobStatus.Created
        self.result = JobResult.Unavailable
    
    def Status(self):
        return self.status
    
    def Result(self):
        return self.result
    
    def onStart(self):
        self.status = JobStatus.WaitingToRun
        pass
    
    def start(self):
        self.status = JobStatus.Runing
        pass
    
    def stop(self, result):
        assert(result != JobResult.Unavailable)
        assert(self.status == JobStatus.Runing)
        self.result = result
        self.status = JobStatus.RunToCompletion
        if self.stop_handler:
            self.stop_handler(self)
        
    def onCancel(self):
        pass
    
    def canceled(self):
        self.status = JobStatus.Canceled
        if self.stop_handler:
            self.stop_handler(self)    
    
    def isCompletion(self):
        return self.status == JobStatus.Canceled or self.status == JobStatus.NoRun or self.status == JobStatus.RunToCompletion 
